package cn.saas.stepview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;


/**
 * @Title :
 * @date : 2021/6/10 14:17
 */
public class ItemView extends ConstraintLayout {

    private Context mContext;
    private String mTitle;
    private boolean mIsNeed;
    private boolean mIsEditable;
    private String mHint;


    private boolean mArrowVisible;
    private int mInputType;
    private Drawable mBackground;
    private int mFontColor;
    private boolean mIsDivideVisible;
    private String mUnit = "";
    private TextView mTvIsNeeded;
    private TextView mTvTitle;
    private EditText mEtContent;
    private ImageView mIvArrow;
    private View mDivide;
    private TextView mTvUnit;

    public ItemView(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public ItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initAttrs(attrs);
        initView();
        initData();
    }


    private void initAttrs(AttributeSet attrs) {
        TypedArray attributes = mContext.obtainStyledAttributes(attrs, R.styleable.ItemView);
        mTitle = attributes.getString(R.styleable.ItemView_iv_title);
        mIsNeed = attributes.getBoolean(R.styleable.ItemView_iv_is_need, false);
        mIsEditable = attributes.getBoolean(R.styleable.ItemView_iv_editable, true);
        mHint = attributes.getString(R.styleable.ItemView_iv_hint);
        mArrowVisible = attributes.getBoolean(R.styleable.ItemView_iv_arrow_visible, false);
        mInputType = attributes.getInt(R.styleable.ItemView_iv_input_type, 1);
        mBackground = attributes.getDrawable(R.styleable.ItemView_iv_background);
        mFontColor = attributes.getColor(R.styleable.ItemView_iv_font_color, Color.parseColor("#999999"));
        mIsDivideVisible = attributes.getBoolean(R.styleable.ItemView_iv_divide, true);
        mUnit = attributes.getString(R.styleable.ItemView_iv_unit);
        attributes.recycle();
    }


    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_item, this, false);
//        View view = View.inflate(mContext,R.layout.view_item, this);
        mTvIsNeeded = view.findViewById(R.id.tv_is_needed);
        mTvTitle = view.findViewById(R.id.tv_title);
        mEtContent = view.findViewById(R.id.et_content);
        mIvArrow = view.findViewById(R.id.iv_arrow);
        mDivide = view.findViewById(R.id.view);
        mTvUnit = view.findViewById(R.id.tv_unit);

        addView(view);
    }


    private void initData() {
        mTvIsNeeded.setVisibility(mIsNeed ? VISIBLE : INVISIBLE);
        mTvTitle.setText(mTitle == null ? "" : mTitle);
        mEtContent.setHint(mHint == null ? "" : mHint);
        mEtContent.setFocusable(mIsEditable);
        mEtContent.setClickable(mIsEditable);
        mEtContent.setFocusableInTouchMode(mIsEditable);

        if (!mIsEditable) {
            mEtContent.setKeyListener(null);
            mEtContent.setMovementMethod(null);
        }
        if (!mArrowVisible) {
            mIvArrow.setVisibility(INVISIBLE);
        } else {
            mIvArrow.setVisibility(mIsEditable ? INVISIBLE : VISIBLE);
        }

        if (mBackground != null) {
            mEtContent.setBackground(mBackground);
            mIvArrow.setVisibility(GONE);
        }
        mEtContent.setTextColor(mFontColor);
        mTvTitle.setTextColor(mFontColor);
        mDivide.setVisibility(mIsDivideVisible ? VISIBLE : INVISIBLE);
            mTvUnit.setText(mUnit);
            mTvUnit.setVisibility(View.VISIBLE);
        setEdittextInputType(mEtContent);
    }

    private void setEdittextInputType(EditText mEtContent) {
        switch (mInputType) {
            case 0:
                mEtContent.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case 1:
                mEtContent.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            case 2:
                mEtContent.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
            default:
                break;
        }
    }

    public String getContent() {
        return mEtContent.getText().toString().trim();
    }

    public void setContent(String str) {
        mEtContent.setText(str == null ? "" : str);
    }

    public EditText getEdit() {
        return mEtContent;
    }

    public void setEditable(boolean isEditable) {
        if (isEditable) {
            mEtContent.setFocusableInTouchMode(true);
            mEtContent.setFocusable(true);
            mEtContent.requestFocus();
        } else {
            mEtContent.setFocusable(false);
            mEtContent.setFocusableInTouchMode(false);
        }
    }
}
